#include <GL/glut.h>
#include "bar.h"
#include "loadModel.h"

/* Lifecycle */

//Constructor
Bar::Bar()
{
	//Default at 10
	max = 10;
	cur =  0;
}

void Bar::draw()
{
	//Create matrix
	glPushMatrix();

	//Local translate
	glTranslatef(loc.x, loc.y, loc.z);

	//Draw
	glBegin(GL_TRIANGLES);

	//For each vertex
	unsigned int i;
	for (i = 0; i < v.size(); i++)
	{
		//Color based on percentage current/max
		glColor3f(
			c[i].x *cur /max, 
			c[i].y *cur /max, 
			c[i].z *cur /max);

		//Vertex
		glVertex3f(v[i].x, v[i].y, v[i].z);
	}

	//Stop drawing
	glEnd();

	//Destroy matrix
	glPopMatrix();
}

/* Actions */

void Bar::increase(unsigned char value)
{
	//Won't reach max
	if (max -cur > value)
		//Increase current
		cur += value;
	//Cap at max
	else cur = max;
}

void Bar::decrease(unsigned char value)
{
	//Won't reach 0
	if (cur > value)
		//Decrease current
		cur -= value;
	//Cap at 0
	else cur = 0;
}

/* Setters */

void Bar::setMax     (unsigned char value) { max = value; }
void Bar::setCurrent (unsigned char value) { cur = value; }
